//
//  HWTextView.m
//  黑马微博2期
//
//  Created by apple on 14-10-20.
//  Copyright (c) 2014年 heima. All rights reserved.
//

#import "HWTextView.h"
//#import "Masonry.h"




@interface HWTextView()

@end


@implementation HWTextView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.textContainerInset = UIEdgeInsetsMake(15, 5, 5, 5);
       
        // 通知
        // 当UITextView的文字发生改变时，UITextView自己会发出一个UITextViewTextDidChangeNotification通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];
        
//        self.backgroundColor = [UIColor blueColor];
        
        
        


        
    }
    return self;
}


- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

/**
 * 监听文字改变
 */
- (void)textDidChange
{
    // 重绘（重新调用）

    [self setNeedsDisplay];
    
}

- (void)setPlaceholder:(NSString *)placeholder
{
    _placeholder = [placeholder copy];
    
    [self setNeedsDisplay];
}

- (void)setPlaceholderColor:(UIColor *)placeholderColor
{
    _placeholderColor = placeholderColor;

    [self setNeedsDisplay];
}

- (void)setText:(NSString *)text
{
    [super setText:text];
    
    // setNeedsDisplay会在下一个消息循环时刻，调用drawRect:
    [self setNeedsDisplay];
}

- (void)setFont:(UIFont *)font
{
    [super setFont:font];
    
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
//    [HWRandomColor set];
//    UIRectFill(CGRectMake(20, 20, 30, 30));
    // 如果有输入文字，就直接返回，不画占位文字
    
    
    // 文字属性
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
#warning 注释了下面这句
    if (self.font){
        attrs[NSFontAttributeName] = self.font;
    }else{
        NSLog(@"%@",self.font);
        attrs[NSFontAttributeName] = [UIFont systemFontOfSize:15];
    }
    
    attrs[NSForegroundColorAttributeName] = self.placeholderColor?self.placeholderColor:[UIColor grayColor];
    
    //文字计数器
//    CGRect rect2 = CGRectMake(self.width-70, self.height -30, 70, 30);
//    NSString *str = [NSString stringWithFormat:@"%lu /200",(unsigned long)self.text.length];
//    [str drawInRect:rect2 withAttributes:attrs];
    
    // 画文字
    //    [self.placeholder drawAtPoint:CGPointMake(5, 8) withAttributes:attrs];
    CGFloat x = 9;
    CGFloat w = rect.size.width - 2 * x;
    CGFloat y = 15;
    CGFloat h = rect.size.height - 2 * y;
    CGRect placeholderRect = CGRectMake(x, y, w, h);
    
    if (self.hasText)
    {
    //        [self.text drawInRect:placeholderRect withAttributes:attrs];
        return;

    }
 
    [self.placeholder drawInRect:placeholderRect withAttributes:attrs];
    
    
    //创建一个贴在底部的view
     
   
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    //_UITextContainerView
    //widthTracksTextView
    
//     NSLog(@"%@",self.subviews);

//    for (UIView *view in self.subviews) {
//        
//        if ([NSStringFromClass([view class]) isEqualToString:@"_UITextContainerView"]) {
////             NSLog(@"xxxxx%@",view.subviews);
//         
//            CGRect rect1 = view.frame;
//            rect1.size.width -=7;
//            
//            view.frame = rect1;
//            //            view.backgroundColor = [UIColor purpleColor];
//            view.transform = CGAffineTransformMakeTranslation(6, 7);
//            
//        }
//    }
    


}





@end
